﻿using CWQ.Api.Common;
using CWQ.Api.Models;
using OF.AppContext;
using OF.Component.BLL;
using OF.Component.DTO;
using OF.Component.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace CWQ.Api.Controllers
{
    /// <summary>
    /// 消息
    /// </summary>
    public class MessageController : BaseApiController
    {
       /// <summary>
       /// 
       /// </summary>
       /// <param name="shopId"></param>
       /// <param name="pageIndex"></param>
       /// <param name="pageSize"></param>
       /// <returns></returns>
        [HttpGet]
        public ResultData<OwnerMessageModel> SearchListPageByShopId(int shopId, int pageIndex, int pageSize)
        {
            var result = ResultData<OwnerMessageModel>.Success("");
            int total = 0;
            OwnerMessageModel model = new OwnerMessageModel();
            List<OwnerMessageDto> list = MessageOperate.SearchListPageByShopId(shopId, pageIndex, pageSize, out total);
            model.List = list;
            model.Total = total;
            result.data = model;
            return result;

        }

        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<PostDetailCommentDto> AddComment(CommentModel model)
        {
            var result = ResultData<PostDetailCommentDto>.Error("评论失败");
            UserDataStatisticsInfo oStatistics = new UserDataStatisticsInfo
            {
                DataTypeID = model.PostType,
                ShopID = model.CommentUserID,
                CreatedDate = DateTime.Now,
                StatisticsType = 2,
                DataID = model.PostId
            };
            UserDataStatisticsOperate.Insert(oStatistics);

            CommentInfo info = new CommentInfo
            {
                CommentType = model.PostType,
                CommentUserID = model.CommentUserID,
                Content = model.CommentContent,
                CreatedDate = DateTime.Now,
                IsActive = 1,
                ContentID = model.PostId
            };
            long cid = CommentOperate.Insert(info);
            //更新动态评论数目
            if (model.PostType == 1)
            {
                var oPostSend = PostSendOperate.GetByID(model.PostId);
                if (oPostSend != null)
                {
                    oPostSend.CommentCount += 1;
                    PostSendOperate.Update(oPostSend);
                }
            }
            else if (model.PostType == 2)
            {
                var oPostReceive = PostReceiveOperate.GetByID(model.PostId);
                if (oPostReceive != null)
                {
                    oPostReceive.CommentCount += 1;
                    PostReceiveOperate.Update(oPostReceive);
                }
            }
            if (cid>0)
            {
                int rid = MessageOperate.Insert(new MessageInfo
                {
                    CreatedDate = DateTime.Now,
                    IsActive = 1,
                    MessageContentID = cid,
                    MessageType = 1,
                    OwnerShopID = model.OwnerShopID,
                    SubjectID = model.PostId,
                    SubjectType = model.PostType,
                    ViewStatus = 1,
                    TriggerShopID = model.CommentUserID
                });
                if (rid > 0)
                {
                    ShopInfo oShop = ShopOperate.GetByID(model.CommentUserID);
                    if (oShop!=null)
                    {
                        string[] arr = new string[] { string.Format("{0}({1})", oShop.UserName, oShop.ShopName), model.CommentContent };
                        result.status = 0;
                        result.message = "评论成功";
                        result.data = new PostDetailCommentDto
                        {
                            Content = model.CommentContent,
                            UserName = oShop.UserName,
                            ShopName = oShop.ShopName,
                             ShopID = oShop.ShopID
                        };
                    }
                   
                }
            }
            return result;
        }

        /// <summary>
        /// 添加点赞
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<PostDetailLikeUserDto> AddLike(LikeModel model)
        {
            var result = ResultData<PostDetailLikeUserDto>.Error("点赞失败");
            if (UserDataStatisticsOperate.QueryList(a=>a.DataTypeID== model.PostType&&a.StatisticsType==1&&a.DataID==model.PostId&&a.ShopID==model.CommentUserID).Count>0)
            {
                result = ResultData<PostDetailLikeUserDto>.Error("点赞重复");
                return result;
            }

            //添加用户行为数据
            UserDataStatisticsInfo info = new UserDataStatisticsInfo
            {
                DataTypeID = model.PostType,
                ShopID = model.CommentUserID,
                CreatedDate = DateTime.Now,
                StatisticsType = 1,
                DataID = model.PostId
            };
            long cid = UserDataStatisticsOperate.Insert(info);
            if (cid > 0)
            {   
                //添加消息
                int rid = MessageOperate.Insert(new MessageInfo
                {
                    CreatedDate = DateTime.Now,
                    IsActive = 1,
                    MessageContentID = 0,
                    MessageType = 2,
                    OwnerShopID = model.OwnerShopID,
                    SubjectID = model.PostId,
                    SubjectType = model.PostType,
                    ViewStatus = 1,
                    TriggerShopID = model.CommentUserID
                });
                //更新动态点赞数目
                if (model.PostType == 1)
                {
                    var oPostSend = PostSendOperate.GetByID(model.PostId);
                    if (oPostSend != null)
                    {
                        oPostSend.LikeCount += 1;
                        PostSendOperate.Update(oPostSend);
                    }
                }
                else if (model.PostType == 2)
                {
                    var oPostReceive = PostReceiveOperate.GetByID(model.PostId);
                    if (oPostReceive != null)
                    {
                        oPostReceive.LikeCount += 1;
                        PostReceiveOperate.Update(oPostReceive);
                    }
                }
                if (rid > 0)
                {
                    ShopInfo oShop = ShopOperate.GetByID(model.CommentUserID);
                    if (oShop != null)
                    {
                        result.status = 0;
                        result.message = "评论成功";
                        result.data = new PostDetailLikeUserDto
                        {
                            ShopName = oShop.ShopName,
                            UserName = oShop.UserName,
                             ShopID = oShop.ShopID
                        };
                    }

                }
            }
            return result;
        }

        /// <summary>
        /// “打电话给我”接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ResultData<int> AddPhoneCall(RequestAddPhoneCall request)
        {
            var result = ResultData<int>.Error("");
            int messageType = 0;
            if (request.FromType == 1)
            {
                messageType = 3;
            }
            else if (request.FromType == 2)
            {
                messageType = 4;
            }
            //插入用户行为统计表
            var oUserStatistics=new UserDataStatisticsInfo();
            oUserStatistics.ShopID = request.CommentUserID;
            if (messageType == 3) //从动态打电话
            {
                oUserStatistics.DataTypeID = request.PostType;
                oUserStatistics.DataID = request.PostId;
                oUserStatistics.StatisticsType = messageType;
            }
            else if(messageType==4)//从店铺打电话
            {
                oUserStatistics.DataTypeID = 3;
                oUserStatistics.DataID = request.OwnerShopID;
                oUserStatistics.StatisticsType = messageType;
            }
            oUserStatistics.CreatedDate = DateTime.Now;
            int nStatisticsID=UserDataStatisticsOperate.Insert(oUserStatistics);

            //插入消息表
            var oMessageInfo =new MessageInfo
            {
                CreatedDate = DateTime.Now,
                IsActive = 1,
                MessageContentID = 0,
                MessageType = messageType,
                OwnerShopID = request.OwnerShopID,
                SubjectID = request.PostId,
                SubjectType = request.PostType,
                ViewStatus = 1,
                TriggerShopID = request.CommentUserID
            };
           
            var nMessageId = MessageOperate.Insert(oMessageInfo);
            result=ResultData<int>.Success(nMessageId);
            return result;
        }

        /// <summary>
        /// 获取店铺消息数量
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultData<int> GetShopMessageCount()
        {
            var shopID = UserContext.Current.UserID;
            var result = ResultData<int>.Success("获取成功");
            int count = MessageOperate.QueryList(a => a.OwnerShopID == shopID&& a.ViewStatus==1).Count;
            result.data = count;
            return result;
        }
        /// <summary>
        /// 设置消息状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultData<string> SetMessageStatus()
        {
            var result = ResultData<string>.Success("");
            var shopId = UserContext.Current.UserID;
            MessageOperate.SetMessageReaded(shopId);
            return result;
        }
    }
}